package com.taotao.taodada.manager;

import com.taotao.taodada.common.ErrorCode;
import com.taotao.taodada.exception.BusinessException;
import com.zhipu.oapi.ClientV4;
import com.zhipu.oapi.Constants;
import com.zhipu.oapi.service.v4.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import io.reactivex.Flowable;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author： HTTT
 * @Date： 2024/6/11 22:16
 * @Describe：
 */
@Component
public class GMLAiManager {
    @Value("${aikey.gml}")
    private String apiKey;
    //稳定的随机数
    private static final float STABLE_TEMPERATURE = 0.05f;
    //不稳定的随机数
    private static final float UNSTABLE_TEMPERATURE = 0.99f;


    /**
     * 流式请求
     * @param systemMessage
     * @param userMessage
     * @return
     */
    public Flowable<ModelData> doFlowRequest(String systemMessage, String userMessage){
        ClientV4 clientV4 = new ClientV4.Builder(apiKey).build();
        ArrayList<ChatMessage> messages = new ArrayList<>();
        ChatMessage systemchatMessage = new ChatMessage(ChatMessageRole.SYSTEM.value(), systemMessage);
        messages.add(systemchatMessage);
        ChatMessage userChatMessage = new ChatMessage(ChatMessageRole.USER.value(), userMessage);
        messages.add(userChatMessage);
        ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()
                .model(Constants.ModelChatGLM4)
                .stream(Boolean.TRUE)
                .invokeMethod(Constants.invokeMethod)
                .messages(messages)
                .build();
        try {
            ModelApiResponse invokeModelApiResp = clientV4.invokeModelApi(chatCompletionRequest);
            return invokeModelApiResp.getFlowable();
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, e.getMessage());
        }
    }

    /**
     * 通用同步不稳定请求
     * @param systemMessage
     * @param userMessage
     * @return
     */
    public String doUnStableRequest(String systemMessage,String userMessage){
        return doRequest(systemMessage,userMessage, Boolean.FALSE,UNSTABLE_TEMPERATURE);
    }

    /**
     * 通用同步稳定请求
     * @param systemMessage
     * @param userMessage
     * @return
     */
    public String doStableRequest(String systemMessage,String userMessage){
        return doRequest(systemMessage,userMessage, Boolean.FALSE,STABLE_TEMPERATURE);
    }

    /**
     * 通用同步请求
     * @param systemMessage
     * @param userMessage
     * @param temperature
     * @return
     */
    public String doRequest(String systemMessage,String userMessage,Float temperature){
        return doRequest(systemMessage,userMessage, Boolean.FALSE,temperature);
    }

    /**
     * 通用请求（简化消息传递）
     * @param systemMessage
     * @param userMessage
     * @param stream
     * @param temperature
     * @return
     */
    public String doRequest(String systemMessage,String userMessage, Boolean stream,Float temperature){
        ClientV4 clientV4 = new ClientV4.Builder(apiKey).build();
        ArrayList<ChatMessage> messages = new ArrayList<>();
        ChatMessage systemchatMessage = new ChatMessage(ChatMessageRole.SYSTEM.value(), systemMessage);
        messages.add(systemchatMessage);
        ChatMessage userChatMessage = new ChatMessage(ChatMessageRole.USER.value(), userMessage);
        messages.add(userChatMessage);
        return doRequest(messages,stream,temperature);
    }

    /**
     * 通用请求
     * @param messages
     * @param stream
     * @param temperature
     * @return
     */
    public String doRequest(List<ChatMessage> messages, Boolean stream,Float temperature){
        ClientV4 clientV4 = new ClientV4.Builder(apiKey).build();
        ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()
                .model(Constants.ModelChatGLM4)
                .stream(stream)
                .temperature(temperature)
                .invokeMethod(Constants.invokeMethod)
                .messages(messages)
                //           .requestId(requestId)
                .build();
        ModelApiResponse invokeModelApiResp = clientV4.invokeModelApi(chatCompletionRequest);
        return invokeModelApiResp.getData().getChoices().get(0).toString();
    }
}
